﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using System.Xml.Serialization;

namespace Common
{
    [XmlRootAttribute("RetInfo")]
    public class RetInfo<T>
    {
        private int m_code = 1;

        public int Code
        {
            get { return m_code; }
            set { m_code = value; }
        }


        private string m_message;

        public string Message
        {
            get { return m_message; }
            set { m_message = value; }
        }


        private T m_attach;

        public T Attach
        {
            get { return m_attach; }
            set { m_attach = value; }
        }


        private bool m_success = false;

        public bool Success
        {
            get { return m_success; }
            set { m_success = value; }
        }



        public static RetInfo<T> DefaultError()
        {
            return new RetInfo<T>() { Code = -1, Message = "有错误发生" };
        }

        public static RetInfo<T> DefaultError(string message)
        {
            return new RetInfo<T>() { Code = -1, Message = message };
        }

        public static RetInfo<T> DefaultError(int code, string message)
        {
            return new RetInfo<T>() { Code = code, Message = message };
        }

        public static RetInfo<T> DefaultSuccess()
        {
            return new RetInfo<T>() { Code = 1, Message = "成功", Success = true };
        }

        public static RetInfo<T> DefaultSuccessWithMessage(string message)
        {
            return new RetInfo<T>() { Code = 1, Message = message, Success = true };
        }

        public static RetInfo<T> DefaultSuccess(T attachData)
        {
            return new RetInfo<T>() { Code = 1, Message = "成功", Success = true, Attach = attachData };
        }

        public static RetInfo<T> RetByCheckDataIsNull(T attachData)
        {
            return attachData == null ? DefaultError("信息不存在") : DefaultSuccess(attachData);
        }
    }




    public class RetInfo
    {
        private int m_code = 1;

        public int Code
        {
            get { return m_code; }
            set { m_code = value; }
        }


        private string m_message;

        public string Message
        {
            get { return m_message; }
            set { m_message = value; }
        }


        private object m_attach;

        public object Attach
        {
            get { return m_attach; }
            set { m_attach = value; }
        }


        private bool m_success = false;

        public bool Success
        {
            get { return m_success; }
            set { m_success = value; }
        }

        public static RetInfo DefaultError()
        {
            return new RetInfo() { Code = -1, Message = "有错误发生" };
        }

        public static RetInfo DefaultError(string message)
        {
            return new RetInfo() { Code = -1, Message = message };
        }

        public static RetInfo DefaultError(int code, string message)
        {
            return new RetInfo() { Code = code, Message = message };
        }

        public static RetInfo DefaultSuccess()
        {
            return new RetInfo() { Code = 1, Message = "成功", Success = true };
        }

        public static RetInfo DefaultSuccessWithMessage(string message)
        {
            return new RetInfo() { Code = 1, Message = message, Success = true };
        }

        public static RetInfo DefaultSuccess(object attachData)
        {
            return new RetInfo() { Code = 1, Message = "成功", Success = true, Attach = attachData };
        }

        public static RetInfo RetByCheckDataIsNull(object attachData)
        {
            return attachData == null ? DefaultError("信息不存在") : DefaultSuccess(attachData);
        }
    }


    public class RetInfoHelper<T>
    {
        public delegate T GetServerResponseData();

        public static RetInfo<T> WebServiceReturn(GetServerResponseData target)
        {
            RetInfo<T> responseResult = null;

            try
            {
                responseResult = RetInfo<T>.DefaultSuccess(target());
            }
            catch (RuntimeException ex)
            {
                responseResult = RetInfo<T>.DefaultError(ex.Message);
            }
            catch (Exception ex)
            {
                responseResult = RetInfo<T>.DefaultError(int.MinValue, ex.Message);
            }

            return responseResult;
        }
    }




    public class RetInfoHelper
    {
        public delegate object GetServerResponseData();
        public delegate void GetServerResponseVoidData();

        public static RetInfo WebServiceReturn(GetServerResponseData target)
        {
            RetInfo responseResult = null;

            try
            {
                responseResult = RetInfo.DefaultSuccess(target());
            }
            catch (RuntimeException ex)
            {
                responseResult = RetInfo.DefaultError(ex.ErrorCode,ex.Message);
            }
            catch (Exception ex)
            {
                responseResult = RetInfo.DefaultError(int.MinValue, ex.Message);
            }

            return responseResult;
        }




        public static RetInfo WebServiceReturn(GetServerResponseVoidData target)
        {
            RetInfo responseResult = null;

            try
            {
                target();
                responseResult = RetInfo.DefaultSuccess();
            }
            catch (RuntimeException ex)
            {
                responseResult = RetInfo.DefaultError(ex.ErrorCode,ex.Message);
            }
            catch (Exception ex)
            {
                responseResult = RetInfo.DefaultError(int.MinValue, ex.Message);
            }

            return responseResult;
        }
    }



}
